
let app = getApp();
Page({
  ...app.globalData.function,
    data: {
      ...app.globalData.config,
        selectDate: "",
        machineShow: false,      
        shopInfo:{},
        comfirmInfo:[],
        shoppingCartList: [],
        remark:"",
        quick_info:{},
        total_money:'0.00',      
        orderType:1,
        carIds:'',       
        info:{
          1:{
            name:'黄先生',
            phone: 183968000,
            address:'福建省厦门市思明区湖滨南路227号之7号（屋后茶山香苑）'
          },
        },
        couponList:{},//优惠券
        remark_list:{},//备注信息,
        discountPrices:{},
        discountIds:{},
        coupon_total:0,//优惠券总金额


        
    },
    onLoad: function (options) {
   
      if(options.value){
        this.setData({
          quick_info:options.value
        })
      }  

      if(options.orderType){
        this.setData({
          orderType:options.orderType,
          carIds:options.carIds
        })
      }

      //this.getConfirm()
     
      // setTimeout(()=>{
      //   this.selectComponent("#page").hideLoading()
      // },2000)


    },
    confirm(e) {
        this.setData({
            selectDate: e.detail.selectDate
        })
    },

    //备注信息
    onChangeremark(event){  
      let storageid=event.currentTarget.dataset.storageid;
      let remark_list=this.data.remark_list
      remark_list[storageid]=event.detail;
      this.setData({
        remark_list
      })
    },

   

    // 计算价格
    computedData(storageId,discountPrice,discountId){
      let couponList=this.data.couponList
      let discountIds=this.data.discountIds
      let discountPrices=this.data.discountPrices     
      let coupon_total=0  
      if(storageId){        
        couponList[storageId]=discountPrice
        discountIds[storageId]=discountId
        discountPrices[storageId]=discountPrice
      }
      Object.keys(discountPrices).forEach(function(key){        
       coupon_total +=parseInt(discountPrices[key])
      })      
      this.setData({couponList,discountIds,discountPrices,coupon_total})
    },

    //提交订单
    wxconfirm(){
      let orderType= this.data.orderType
      let obj_data={
        token:this.getStorage('user_info').wx_openid,
        type:this.data.orderType, //订单类型     
        remark:JSON.stringify(this.data.remark_list), //备注
        carId:this.data.carIds //购物车信息
      }

      if(this.data.discountIds){
        let discountIds=this.data.discountIds
        let redIds=[];
        Object.keys(discountIds).forEach(function(key){
          redIds.push(discountIds[key])
        })
        obj_data.redId=redIds.toString()
      }      
      if(!this.data.comfirmInfo.address.id){
        wx.showToast({title:"请选择收货地址！",icon:"none"})
        return
      }
      obj_data.addId=this.data.comfirmInfo.address.id   
      
      
      if(orderType=='carts'){ //购物车信息         
        this.orderSubmit(obj_data).then(res=>{
          if(res.resCode=="9999"){
            this.router("/pagesOrder/wxPay/index?order_no="+res.data.order_no+"&goods_total="+res.data.goods_total+"&orderpay=1")
          }else{
            wx.showToast({title:res.resMessage,icon:"none"})
          }
        })
      }else{
        let goodsInfo=JSON.parse(this.data.quick_info)
        obj_data.specId=goodsInfo.specId
        obj_data.goodsId=goodsInfo.goodsId
        obj_data.number=goodsInfo.number,
        obj_data.is_share=goodsInfo.is_share?1:0,
        obj_data.recommendId=goodsInfo.recommendId?goodsInfo.recommendId:0,
        obj_data.share_money=goodsInfo.share_money?goodsInfo.share_money:0,

        // console.log(obj_data)
        // return 

        
        this.orderSubmit(obj_data).then(res=>{
          if(res.resCode=="9999"){
            this.router("/pagesOrder/wxPay/index?order_no="+res.data.order_no+"&goods_total="+res.data.goods_total+"&orderpay=1")
          }else{
            wx.showToast({title:res.resMessage,icon:"none"})
          }
        })
      }



    },

    //获取订单信息
    getConfirm(){     
      let that=this
      let obj_data={       
        token:this.getStorage('user_info').wx_openid,       
      }
      let orderType= this.data.orderType      
      if(orderType=='carts'){ //购物车信息
        obj_data.carId=this.data.carIds
        this.previewcar(obj_data).then(res=>{          
          if(res.resCode=="9999"){
            that.setData({
              comfirmInfo:res.data,             
              shoppingCartList:res.data.show,
              total_money:res.data.allMoney,
              coupon_list:res.data.coupon_list,
              remark_list:res.data.remark_list,
            })
          }else{
            wx.showToast({title:res.resMessage,icon:"none"})
            setTimeout(()=>{
              wx.navigateBack({
                delta: 1, // 返回上一级页面。
                success: function() {
                    console.log('成功！')
                }
              })
            },2000)
          }
        })
      }else{
        //立即购买 quick       
        let quick_info=JSON.parse(this.data.quick_info)       
        let obj_data={
          token:this.getStorage('user_info').wx_openid,
          goodsId:quick_info.goodsId,
          specId:quick_info.specId,
          number:quick_info.number,
          is_share:quick_info.is_share?1:0,
          recommendId:quick_info.recommendId?quick_info.recommendId:0,
          share_money:quick_info.share_money?quick_info.share_money:0,
        }
        this.preView(obj_data).then(res=>{
          if(res.resCode=="9999"){
            that.setData({
              comfirmInfo:res.data,             
              shoppingCartList:res.data.show,
              total_money:res.data.allMoney,
              coupon_list:res.data.coupon_list,
              remark_list:res.data.remark_list,
            })
          }else{
            wx.showToast({title:res.resMessage,icon:"none"})
            setTimeout(()=>{
              wx.navigateBack({
                delta: 1, // 返回上一级页面。
                success: function() {
                    console.log('成功！')
                }
              })
            },2000)
          }
        })
      }   
    },

    // 优惠券跳转
    routeToCoupon(event){
      let coupon_list=event.currentTarget.dataset.value;
      let storageid=event.currentTarget.dataset.storageid;
      let discountId=this.data.discountIds[storageid]?this.data.discountIds[storageid]:0
      let discountPrice=this.data.discountPrices[storageid]?this.data.discountPrices[storageid]:0
      let couponList=JSON.stringify(coupon_list)
      wx.navigateTo({
        url: "/pagesOrder/discount-coupon/index?flag=1&storageId="+storageid+"&discountId="+discountId+"&discountPrice="+discountPrice+"&coupon_list="+couponList,
      })
    },
    //下拉刷新
  onPullDownRefresh() {
    // // 停止下拉刷新效果
    // console.log('ok',11);
    this.onShow()
     wx.stopPullDownRefresh()     

   },
   onShow(){
     this.getConfirm()
   }
    
})